package com.fasteat.mall.quartz;

import com.fasteat.mall.entity.Product;
import com.fasteat.mall.mapper.ProductMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务
 * 从数据库查询前十好评的商品，并将其保存在redis中
 */
@Component
public class UpdateTenGoodProductTask extends TaskSupport implements Job {

	@Autowired
	private RedisTemplate redisTemplate;

	@Autowired
	private ProductMapper productMapper;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		System.out.println("定时任务执行了");
		List<Product> list = productMapper.selectUnionOtherTable();
		redisTemplate.opsForValue().set("TEN_GOOD_PRODUCT",list);
	}

	@Override
	public Rule setRule() {
		return new Rule("GoodProductTask", "MallGroup",
				this.getClass(), "59 59 23 * * ?");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		loadRule();
	}
}
